﻿using CKS2320.Common.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.PeerToPeer.Collaboration;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CKS2412.Controller
{
    public partial class FrmOeeConfig : Form
    {
        public FrmOeeConfig()
        {
            InitializeComponent();
        }

        string filePath = "D:\\KAISHI\\api\\OeeConfig.json";

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <returns></returns>
        public OeeConfig OeeConfigLoad()
        {
            //string filePath = "D:\\KAISHI\\api\\OeeConfig.json";
            string text = File.ReadAllText(filePath);
            return JsonConvert.DeserializeObject<OeeConfig>(text);
        }

        private void FrmOeeConfig_Load(object sender, EventArgs e)
        {
            OeeConfig oee = OeeConfigLoad();
            if (oee != null)
            {
                txtPlanTime.Text = oee.ProductionPlanTime?.ToString("0.00");
                txtRhythm.Text = oee.Rhythm?.ToString("0.00");
                txtTotalOutput.Text = oee.TotalCountPlan?.ToString("0.00");
            }
            else
            {
                txtPlanTime.Text = "";
                txtRhythm.Text = "";
                txtTotalOutput.Text = "";
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                OeeConfig oee = new OeeConfig();
                oee.ProductionPlanTime = Convert.ToSingle(txtPlanTime.Text);
                oee.Rhythm = Convert.ToSingle(txtRhythm.Text);
                oee.TotalCountPlan = Convert.ToSingle(txtTotalOutput.Text);
                string content = JsonConvert.SerializeObject(oee);
                File.WriteAllText(filePath, content);
                MessageBox.Show("OK!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something Error!" + ex.Message.ToString());
            }
        }
    }
}
